﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace Founder.PKURG.CRMCommon
{
    public class DbCommandUtility
    {
        public static DbCommand BuildCommand(Database db, string sqlQuery, bool isStoredProcedure, List<DBCommandInputParameter> inputParameterList)
        {
            DbCommand result = DbCommandUtility.BuildCommand(db, sqlQuery, isStoredProcedure);
            if (isStoredProcedure == true)
            {
                foreach (DBCommandInputParameter inputParameter in inputParameterList)
                {
                    DbCommandUtility.AddCommandInputParameter(db, result, inputParameter.Name, inputParameter.Type, inputParameter.Value);
                }
            }
            return result;
        }

        public static DbCommand BuildCommand(Database db, string sqlQuery, bool isStoredProcedure,
          List<DBCommandInputParameter> inputParameterList,List<DBCommandOutputParameter> outputParaList)
        {
            DbCommand result = DbCommandUtility.BuildCommand(db, sqlQuery, isStoredProcedure);
            if (isStoredProcedure == true)
            {
                foreach (DBCommandInputParameter inputParameter in inputParameterList)
                {
                    DbCommandUtility.AddCommandInputParameter(db, result, inputParameter.Name, inputParameter.Type, inputParameter.Value);
                }
                foreach (DBCommandOutputParameter outPutPara in outputParaList)
                {
                    DbCommandUtility.AddCommandOutputParameter(db, result, outPutPara);
                }
            }
            return result;
        }

        public static DbCommand BuildCommand(Database db, string sqlQuery, bool isStoredProcedure)
        {
            if (db == null)
            {
                throw new Exception("数据库变量为空");
            }

            DbCommand result = null;
            if (isStoredProcedure == true)
            {
                result = db.GetStoredProcCommand(sqlQuery);
            }
            else
            {
                result = db.GetSqlStringCommand(sqlQuery);
            }

            return result;
        }

        public static void AddCommandInputParameter(Database db, DbCommand command, DBCommandInputParameter param)
        {
            AddCommandInputParameter(db, command, param.Name, param.Type, param.Value);
        }

        public static void AddCommandInputParameter(Database db, DbCommand command, string paramName, DbType dbType, object value)
        {
            db.AddInParameter(command, paramName, dbType, value);
        }


        public static void AddCommandOutputParameter(Database db, DbCommand command, DBCommandOutputParameter param)
        {
            AddCommandOutputParameter(db, command, param.Name, param.Type, param.Size);
        }

        public static void AddCommandOutputParameter(Database db, DbCommand command, string paramName, DbType dbType,int size)
        {
           
            db.AddOutParameter(command, paramName, dbType, size);
        }



    }
}